<?php
	include('Conexion.php');
	global $alumno_usuario;
	$alumno_usuario = $_COOKIE['id'];
	$usuarioEmail = $_COOKIE['username'];
	$template = $_COOKIE['template'];

	$con = mysql_connect($GLOBALS["server"],$GLOBALS["host"],$GLOBALS["pass"]) or die ('Ocurri&oacute; el siguiente error en la conexi&oacute;n: ' . mysql_error());
	mysql_select_db($GLOBALS["database"],$con);
	
	$anuncio = false;
	
	if (isset($_POST['btnEstilo-submit'])) {
	$templateIDRows = mysql_query("SELECT Temp_Clave FROM Template WHERE Temp_Nombre = '".$template."'", $con) or die("MySQL Login Error: ".mysql_error());
	$templateID = mysql_fetch_array($templateIDRows);
	mysql_query("UPDATE Alumno SET Alu_Template = ".$templateID[0]." WHERE Alu_Email = '".$usuarioEmail."';", $con) or die("MySQL Login Error: ".mysql_error());
	print('<p class="p-center" style="color:#C90;">Tu plantilla ha sido actualizada</p>');
	}
	
	
if (isset($_POST['btn-submit'])) {
	$name = $_POST['field-name'];
	$lastName = $_POST['field-lastName'];
	$sex = $_POST['radio-sex'];
	$mail = $_POST['field-mail'];
	$pwd = $_POST['field-pwd'];
	$repwd = $_POST['field-repwd'];
	$entrar = true;
	
	if($name == "") $entrar = false; 
	if($lastName == "") $entrar = false; 
	if($sex == "") $entrar = false; 
	if($mail == "") $entrar = false; 
	if($pwd == "") $entrar = false; 
	if($repwd == "") $entrar = false; 
	if($pwd != $repwd) $entrar = false; 
	
	if($entrar){
		mysql_query("UPDATE alumno SET Alu_Nombre='".$name."', Alu_Apell='".$lastName."', Alu_Sexo='".$sex."', Alu_Email='".$mail."' ,Alu_Pass='".$pwd."' WHERE Alu_ID='".$alumno_usuario."'", $con) or die("MySQL Login Error: ".mysql_error());
	}
	mysql_close();
	$anuncio = true;
}
?>
<h1>Tus datos</h1>
<?php
	if ($anuncio) {printf("<p class='p-center' style='color:#C90;'>Tus datos han sido actualizados</p>");}
?>
<form action="Home.php?page=perfil" method="post">
<?php
$resultado = mysql_query("SELECT Alu_Nombre,Alu_Apell,Alu_Sexo,Alu_Email,Alu_Pass FROM alumno WHERE Alu_ID='".$alumno_usuario."'", $con) or die("MySQL Login Error: ".mysql_error());
	
	$row = mysql_fetch_array($resultado);
	$name = $row["0"];
	$lastName = $row["1"];
	$sex = $row["2"];
	$mail = $row["3"];
	$pwd = $row["4"];
?>
<label for="field-name">Nombre(s):</label>
<input type="text" id="field-name" name="field-name" value="<?php echo $name ?>" class="required"/>
<span name="field-name-span"><?php if(isset($_POST['btn-submit']) and $_POST['field-name'] == "") echo "Favor de ingresar Nombre." ?></span>
<br />
<label for="field-lastName">Apellido(s):</label>
<input type="text" id="field-lastName" name="field-lastName" value="<?php echo $lastName ?>" class="required"/>
<span name="field-lastName-span"><?php if(isset($_POST['btn-submit']) and $_POST['field-lastName'] == "") echo "Favor de ingresar Apellido." ?></span>
<br />
<label for="field-mail">Correo Electr&oacute;nico:</label>
<input type="text" id="field-mail" name="field-mail" value="<?php echo $mail ?>"/>
<span name="field-pwd-span"><?php if(isset($_POST['btn-submit']) and $_POST['field-mail'] == "") echo "Favor de ingresar su correo." ?></span>
<br />
<label for="field-pwd">Contrase&ntilde;a:</label>
<input type="password" id="field-pwd" name="field-pwd" value="<?php echo $pwd ?>"/>
<span name="field-pwd-span"><?php if(isset($_POST['btn-submit']) and $_POST['field-pwd'] == "") echo "Favor de ingresar su contrase&ntilde;a." ?></span>
<br />
<label for="field-repwd">Reescribir Contrase&ntilde;a:</label>
<input type="password" id="field-repwd" name="field-repwd" value="<?php echo $pwd ?>"/>
<span name="field-pwd-span"><?php if(isset($_POST['btn-submit']) and $_POST['field-repwd'] == "") echo "Favor de volver a escribir su contrase&ntilde;a."; if(isset($_POST['btn-submit']) and $_POST['field-pwd'] != $_POST['field-repwd']) echo "Favor de volver a escribir su contrase&ntilde;a.";?></span>
<br />
<fieldset><legend>Sexo</legend>
<?php
	if($sex == "M"){
?>
<input type="radio" id="radio-sex" name="radio-sex" value="M" checked/> Hombre
<input type="radio" id="radio-sex" name="radio-sex" value="F"/> Mujer
<br />
<?php
	} else {
?>
<input type="radio" id="radio-sex" name="radio-sex" value="M"/> Hombre
<input type="radio" id="radio-sex" name="radio-sex" value="F"checked/> Mujer
<br />
<?php
	} 
?>
</fieldset>
<input type="submit" id="btn-submit" name="btn-submit" value="Actualizar" onclick="validar"/>
<div class="clear"></div>
</form>